package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.BlogDetail;
import com.hemaapp.yjnh.view.NumberSelectorLayout;

import java.text.DecimalFormat;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomSharedPreferencesUtil;
import xtom.frame.util.XtomToastUtil;

/**
 * 预约填写订单第一步
 *
 * @author ZhangGuiyou
 *         Date:2015-9-8 16:31
 */
public class BookConfirm1Activity extends BaseActivity implements OnClickListener {

    private final static int REQUEST_DATE = 0x01;
    @Bind(R.id.title_left_btn)
    ImageButton titleLeftBtn;
    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.edt_contact)
    EditText edtContact;
    @Bind(R.id.edt_mobile)
    EditText edtMobile;
    @Bind(R.id.tv_goods_name)
    TextView tvGoodsName;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.layout_number_selector)
    NumberSelectorLayout numberSelector;
    @Bind(R.id.tv_total)
    TextView tvTotal;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;

    private BlogDetail blog;//当前商品
    private Double price = 1.0;
    private Double total = 0.0;//总费用
    DecimalFormat df = new DecimalFormat("#.00");//保留两位小数
    private int buynum = 1;//够买数量

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_book_confirm_1);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        if (blog != null) {
            price = Double.valueOf(blog.getPrice());
            total = price;
            tvGoodsName.setText(blog.getName());
            tvPrice.setText("¥" + df.format(price));
            tvTotal.setText("¥" + df.format(total));
            numberSelector.setNumber(buynum);
        }
        edtContact.setText(XtomSharedPreferencesUtil.get(mContext, "bespeak_name"));
        edtMobile.setText(XtomSharedPreferencesUtil.get(mContext, "bespeak_mobile"));
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void getExras() {
        blog = (BlogDetail) mIntent.getSerializableExtra("blog");
    }

    @Override
    protected void setListener() {
        titleText.setText("填写订单");
        titleRightBtn.setVisibility(View.GONE);
        BaseUtil.setEmojiFilter(edtContact);
        numberSelector.setNumberSelectorListener(new NumberSelectorLayout.NumberSelectorListener() {
            @Override
            public void onNumberChanged(int curValue) {
                buynum = curValue;
                total = buynum * price;
                tvTotal.setText("¥" + df.format(total));
            }
        });
    }

    @OnClick({R.id.title_left_btn, R.id.tv_submit, R.id.tv_date})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.tv_submit:
                String name = edtContact.getText().toString();
                String mobile = edtMobile.getText().toString();
                String date = tvDate.getText().toString();
                if (isNull(name)) {
                    XtomToastUtil.showShortToast(mContext, "请填写预约人姓名");
                    break;
                }
//                if (!uuUtils.judgeMobileNum(mobile)) {
//                    XtomToastUtil.showShortToast(mContext, "请填写有效的手机号码");
//                    break;
//                }
                if (isNull(date)) {
                    XtomToastUtil.showShortToast(mContext, "请选择出发日期");
                    break;
                }
                if (buynum == 0) {
                    XtomToastUtil.showShortToast(mContext, "人数不能为空");
                    break;
                }
                //记录这次的联系人和手机号
                XtomSharedPreferencesUtil.save(mContext, "bespeak_name", name);
                XtomSharedPreferencesUtil.save(mContext, "bespeak_mobile", mobile);
                //跳转到确认支付界面
                Intent intent = new Intent(mContext, BookConfirm2Activity.class);
                intent.putExtra("keytype", "4");//1：从购物车中来 2：立即购买 3：定制订单 4：走走订单
                intent.putExtra("blog", blog);// 走走信息
                intent.putExtra("buycount", buynum);// 够买商品数量
                intent.putExtra("date", date);// 开始日期
                intent.putExtra("name", name);
                intent.putExtra("mobile", mobile);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_date:
                Intent itCalendar = new Intent(mContext, CalendarActivity.class);
                itCalendar.putExtra("id", blog.getId());
                itCalendar.putExtra("style", 1);
                startActivityForResult(itCalendar, REQUEST_DATE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (RESULT_OK != resultCode) {
            return;
        }
        switch (requestCode) {
            case REQUEST_DATE:
                tvDate.setText(data.getStringExtra("date"));
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
